//function Polyline(data, sketch, options) {
function Polyline(data, overlay, options) {
	var self = this;
	if(data) {
		self.points = data.points;
		self.color = data.color; 
		self.weight =data.weight;
		self.zoom = data.zoom;
	} 
	else {
		self.points = [];
		self.pointIndexsToKeep = [];
		self.color = "#FF0000"; 
		self.weight =4;
		self.zoom = 8;
	}

	// if (sketch) {
		// self.sketch = sketch;
		// $(self.sketch).bind('delete', function() {
			// self.onSketchDelete();
		// })
	// }
	
	if (overlay) {
		self.overlay = overlay;
		$(self.overlay).bind('delete', function() {
			self.onOverlayDelete();
		})
	}

	this.options = options || {};
  
  
}

//Polyline.prototype.onSketchDelete = function() {
Polyline.prototype.onOverlayDelete = function() {
	if(self.gpolyline) {
		self.gpolyline.setMap(null);
	}
}

Polyline.prototype.addPoint = function(position) {
	var self = this;
	self.points.push(position);
	if(self.gpolyline) {
		setTimeout(function() {
			var map = self.gpolyline.getMap();
			var oldGPolyline = self.gpolyline;
			self.gpolyline = self.to_gpolyline();
			self.gpolyline.setMap(map);
			setTimeout(function() {
				oldGPolyline.setMap(null);
				}, 
				0.1
			);
			}
			,0
		);
	}
} 

Polyline.prototype.to_gpolyline = function() {
	var self = this;
	var gpolyline = new google.maps.Polyline({
		path: _.map(self.points, Polyline.posToGPoint)
		, strokeColor: self.color
		, strokeOpacity: 1.0
		, strokeWeight: self.weight 
	});

	if (self.options.click) {
		google.maps.event.addListener(gpolyline, 'click', function() {
			// self.options.click.call(self.sketch, self);
			 self.options.click.call(self.overlay, self);
		});
	  
	}

	return gpolyline;
} 

Polyline.posToGPoint = function(pos) {
	return new google.maps.LatLng(pos.latitude, pos.longitude);
}

Polyline.prototype.setMap = function(map) {
	if(!this.gpolyline) this.gpolyline = this.to_gpolyline();
	this.gpolyline.setMap( map && map.gmap);
} 

Polyline.prototype.to_json = function() {
	this.zip2();
	return {
		color: this.color
		, points: this.points
		,weight: this.weight
	};
}

Polyline.prototype.zip = function() {
	
	var zip = [];
	var step = 15;
	var cont= 0;
	var length = this.points.length;
	if(length == 0)
		return;
	
	for(var i=0;i<length;i+=step){
		zip.push(this.points[i]);
		cont++;		
	}
		
	zip.push(this.points[length-1]);
	
	this.points = zip;
	
}

Polyline.prototype.zip2 = function () {
	
	var tolerance = this.getTolerance();
	var dpr = new Dpr(this.points,tolerance);
	this.points = dpr.get();
	
	
}

Polyline.prototype.getTolerance = function(){
	
	switch (this.zoom) {
		case 21:
			return 0.000001;
		case 20:
			return 0.000003; //testeado
		case 19:
			return 0.000007; //testeado
		case 18:
			return 0.00001; //testeado
		case 17:
			return 0.00005;	//testeado
		case 16:
			return 0.00005;	//testeado
		case 15:
			return 0.0001; //testeado
		case 14:
			return 0.0002; //testeado
		case 13:
			return 0.0007; //testeado
		case 12:
			return 0.001; //testeado	
		case 11:
			return 0.003; //testeado
		case 10:
			return 0.002; //testeado
		case 9:
			return 0.0095; //testeado	
		case 8:
			return 0.03; //testeado
		case 7:
			return 0.04; //testeado
		case 6:
			return 0.1; //testeado
		case 5:
			return 0.3; //testeado
		case 4:
			return 0.6; //testeado
		case 3:
			return 1; //testeado
		case 2:
			return 1.5; //testeado
		case 1:
			return 3; //testeado
		case 0:
			return 5; //testeado
		default:
			return 0.0001;
	}
}






